package search;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;

public class TwitterIndexer {
	private File m_indexDir;

	public TwitterIndexer(File indexDir) {
		m_indexDir = indexDir;
	}

	public void index(File file, boolean create) {
		try {
			IndexWriter writer = new IndexWriter(m_indexDir,
					new StandardAnalyzer(), create,
					IndexWriter.MaxFieldLength.UNLIMITED);
			TwitterDataReader reader = new TwitterDataReader();
			reader.open(file);
			while (reader.next() >= 0) {
				TwitterRecord record = reader.getRecord();
				Document document = new Document();
				document.add(new Field("id", record.m_id, Field.Store.COMPRESS, Field.Index.NOT_ANALYZED));
				document.add(new Field("user", record.m_user, Field.Store.COMPRESS, Field.Index.NO));
				document.add(new Field("time", record.m_time, Field.Store.COMPRESS, Field.Index.NO));
				document.add(new Field("text", record.m_text, Field.Store.COMPRESS, Field.Index.ANALYZED));
				writer.addDocument(document);
			}
			writer.close();
			reader.close();
		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		TwitterIndexer indexer = new TwitterIndexer(new File(args[0]));
		indexer.index(new File(args[1]), true);
	}
}
